//
//  MYWelcomeViewController.swift
//  Sina
//
//  Created by 王亮龙1 on 16/5/24.
//  Copyright © 2016年 lianglong. All rights reserved.
//

import UIKit
import SVProgressHUD
import SDWebImage
import SnapKit

class MYWelcomeViewController: UIViewController {
    
    
    
    override func loadView() {
        
        view = backGroundView
    }
    
    
    
    

    override func viewDidLoad() {
        super.viewDidLoad()
        view.backgroundColor = UIColor.cyanColor()
        
        setupUI()
    }
    
    
    
    //设置界面
    private func setupUI() {
        view.addSubview(userIcon)
        view.addSubview(nameLabel)
        
        userIcon.snp_makeConstraints { (make) in
            make.centerX.equalTo(self.view.snp_centerX)
            make.bottom.equalTo(self.view.snp_bottom).offset(-200)
            make.width.equalTo(90)
            make.height.equalTo(90)
        }
        
        nameLabel.snp_makeConstraints { (make) in
            make.centerX.equalTo(self.view.snp_centerX)
            make.top.equalTo(userIcon.snp_bottom).offset(16)
        }
        
        userIcon.sd_setImageWithURL(MYUserAccountViewModel.shareUserAccountViewModel.userIcon)
        
        userIcon.cornerRadius = 45
        userIcon.layer.masksToBounds = true
        
    }
    
    
    //在viewDidAppear中开始动画
    override func viewDidAppear(animated: Bool) {
        super.viewDidAppear(animated)
        
        startAnimation()
    }
    
    //开始动画的方法
    private func startAnimation() {
        
        let offset = -UIScreenHeight + 200
        
        
        UIView.animateWithDuration(1.2, delay: 0, usingSpringWithDamping: 0.8, initialSpringVelocity: 6, options: [], animations: { 
            
           self.userIcon.snp_updateConstraints(closure: { (make) in
                make.bottom.equalTo(offset)
            })
            
            self.view.layoutIfNeeded()
            
            }) { (_) in
                
                UIView.animateWithDuration(0.5, animations: { 
                    
                    self.nameLabel.alpha = 1
                    }, completion: { (_) in
                        
                        NSNotificationCenter.defaultCenter().postNotificationName(SwitchRootViewController, object: "main")
                        
                })
        }
    }
    
    
    //懒加载子控件
    private lazy var backGroundView: UIImageView = UIImageView(image: UIImage(named: "ad_background"))
    
    private lazy var userIcon: UIImageView = UIImageView(image: UIImage(named: "avatar_default_big"))
    
    private lazy var nameLabel: UILabel = {
        
        let nameLbl = UILabel()
        
        nameLbl.text = MYUserAccountViewModel.shareUserAccountViewModel.userAccount?.name
        nameLbl.font = UIFont.systemFontOfSize(18.0)
        nameLbl.textColor = UIColor.darkGrayColor()
        nameLbl.alpha = 0
        nameLbl.sizeToFit()
        
        return nameLbl
    }()
    

    

    
}
